﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using NomClasses;

namespace NomCafe
{
    public partial class frmModeManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string nameMode;

        public frmModeManager()
        {
            InitializeComponent();
        }

        private void frmModeManager_Load(object sender, EventArgs e)
        {
            getDataMode();
        }

        private void getDataMode()
        {
            listViewMode.Items.Clear();

            // Lấy danh sách quy cách cho listViewMode            
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_specification> modeList = nc.nom_specifications;
            foreach (NomClasses.nom_specification mode in modeList)
            {
                listViewMode.Items.Add(mode.specification).SubItems.Add(mode.note);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnRenew_Click(object sender, EventArgs e)
        {
            getDataMode();

            txtNameMode.Text = "";
            txtNoteMode.Text = "";

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnAccept.Enabled = true;
            btnRenew.Enabled = true;
        }

        private void listViewMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnAccept.Enabled = false;

            nameMode = "";
            for (int i = 0; i < listViewMode.Items.Count; i++)
            {
                if (listViewMode.Items[i].Selected)
                {
                    nameMode = listViewMode.Items[i].SubItems[0].Text;
                    txtNameMode.Text = nameMode;
                    txtNoteMode.Text = listViewMode.Items[i].SubItems[1].Text;
                }
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtNameMode.Text != "")
                {
                    NomDataContext nc = new NomDataContext();
                    var modeList = from mode in nc.nom_specifications
                                   select mode;
                    foreach (var i in modeList)
                    {
                        if (txtNameMode.Text.Trim().ToLower().Equals(i.specification.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_MODE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_specification spec = new nom_specification();
                        spec.specification = txtNameMode.Text;
                        spec.note = txtNoteMode.Text;
                        nc.nom_specifications.InsertOnSubmit(spec);
                        nc.SubmitChanges();

                        getDataMode();

                        txtNameMode.Text = "";
                        txtNoteMode.Text = "";
                        txtNameMode.Focus();
                    }
                }
                else
                {
                    MessageBox.Show(languageManager.INSERT_MODE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                NomDataContext nc = new NomDataContext();
                nom_specification spec = nc.nom_specifications.Single(i => i.specification == nameMode);
                nc.nom_specifications.DeleteOnSubmit(spec);
                nc.SubmitChanges();

                btnRenew_Click(sender, e);
            }
            catch (Exception)
            {
                MessageBox.Show(languageManager.DELETE_MODE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                NomDataContext nc = new NomDataContext();
                var modeList = from mode in nc.nom_specifications
                               where mode.specification != nameMode
                               select mode;
                    foreach (var i in modeList)
                    {
                        if (txtNameMode.Text.Trim().ToLower().Equals(i.specification.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_MODE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_specification spec = nc.nom_specifications.Single(i => i.specification == nameMode);
                        spec.specification = txtNameMode.Text;
                        spec.note = txtNoteMode.Text;
                        nc.SubmitChanges();

                        btnRenew_Click(sender, e);
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        
    }
}
